from rest_framework import permissions, filters, response, status
from apps.api.config.models import ConfigModel
from apps.api.config.serializers import ConfigModelSerializer
from apps.api.interface.models import InterfaceModel
from utils import viewsets


class ConfigViewSet(viewsets.MyViewSets):
    queryset = ConfigModel.objects.all()
    serializer_class = ConfigModelSerializer
    permission_classes = [permissions.IsAuthenticated]
    filter_backends = [filters.OrderingFilter]
    ordering_fields = ['create_time']

    def list(self, request, *args, **kwargs):
        result = super().list(request, *args, **kwargs)
        for item in result.data["data"]["results"]:
            item["project_id"] = InterfaceModel.objects.filter(id=item["interface_id"]).first().project_id
        return response.Response(result.data, status=status.HTTP_200_OK)
